home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 18 / CU Amiga Magazine's Super CD-ROM 18 (1997)(EMAP Images)(GB)[!][issue 1998-01].iso / CUCD / Programming / AmigaE / Src / Afc / Sort_Example.e < prev    next >
Encoding:
Text File  |  1997-09-09  |  1.9 KB  |  98 lines

  1. /*
  2.     NodeMaster_Sort.e - V1.00
  3.  
  4.     Written By Andrea Galimberti
  5.  
  6.     This source demonstates how to create
  7.     sort a list of "objects". In this case
  8.     our "object" will be called "stuff" and
  9.     will contain just one field called "avg".
  10.  
  11.     Our comparison routine will take as parameters
  12.     two "Stuff" items and the info param (which will be
  13.     ignored).
  14.  
  15.     This source shows also the version() method.
  16.  
  17.  
  18.     This source is Public Domain.
  19.  
  20.     NodeMaster is part of the Amiga Foundation Classes
  21.     See:
  22.           http://www.intercom.it/~fsoft/afc.html
  23.  
  24.     for more info and more modules.
  25. */
  26.  
  27. MODULE 'afc/nodemaster',
  28.        'afc/explain_exception'
  29.  
  30. CONST MAX_ELEM = 50  -> Change this value TO increase/decrease no. of items TO sort
  31.  
  32. OBJECT stuff
  33.   avg
  34. ENDOBJECT
  35.  
  36. /*
  37.     This is our comparison routine.
  38.     look:
  39.  
  40.         a and b are two "stuff" objects.
  41.  
  42.     Their ".avt" field just contains a number:
  43.     by sub a.avg-b.avg will have a value < or > or = to 0
  44.     That we'll return as result.
  45.  
  46.     This is a simple example, but you can create better
  47.     and more complex ones.
  48. */
  49. PROC cpstuff(a:PTR TO stuff,b:PTR TO stuff, info) IS a.avg - b.avg
  50.  
  51.  
  52. PROC main() HANDLE
  53.   DEF k
  54.   DEF num:PTR TO nodemaster, l:PTR TO stuff
  55.   DEF v,r
  56.  
  57.   NEW num.nodemaster()
  58.  
  59.   -> Here we fill the list with random numbers
  60.   Rnd(-RndQ($FFA354B2))
  61.   FOR k:=0 TO MAX_ELEM-1
  62.     NEW l
  63.     l.avg:=Rnd(1000)
  64.     num.add(l)
  65.   ENDFOR
  66.  
  67.   WriteF('Now sorting...')
  68.  
  69.   -> and here we sort it!!!
  70.   num.sort({cpstuff})
  71.  
  72.   WriteF('Done!\n')
  73.  
  74.   -> Let's see the result
  75.   l:=num.first()
  76.   REPEAT
  77.     WriteF('\d\n',l.avg)
  78.   UNTIL (l:=num.succ())=FALSE
  79.  
  80.  
  81.   -> Good: which version of NodeMaster is this?
  82.   v,r := num.version()
  83.  
  84.   WriteF('NodeMaster V\d.\d \n', v,r)
  85.  
  86. EXCEPT DO
  87.   explain_exception()
  88.  
  89.   IF (l:=num.first())<>FALSE
  90.     REPEAT
  91.       Dispose(l)
  92.     UNTIL (l:=num.succ())=FALSE
  93.   ENDIF
  94.   END num
  95.   CleanUp(0)
  96. ENDPROC
  97.  
  98.